package com.cooper.controllers.item;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.cooper.constants.DictConstants;
import com.cooper.constants.ResourceConstants;
import com.cooper.controllers.Controller;
import com.cooper.domain.ImageItem;
import com.cooper.domain.PackageItem;
import com.cooper.entity.DesignerEntity;
import com.cooper.entity.ItemImage;
import com.cooper.entity.ItemInfo;
import com.cooper.entity.ItemSku;
import com.cooper.entity.UserInfo;
import com.cooper.helper.ItemExtendHelper;
import com.cooper.service.admin.AddressService;
import com.cooper.service.core.DictService;
import com.cooper.service.item.ItemRepository;
import com.cooper.service.seller.DesignerService;
import com.cooper.util.Controllers;
import com.cooper.vo.AddressVo;
import com.cooper.vo.ItemInfoVo;
import com.google.common.collect.Lists;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.rest.Post;

public class PreviewController extends Controller{
	
	@Autowired
	private ItemRepository itemRepository;
	
	@Autowired
	private DictService dictService;
	
	@Autowired
	private DesignerService designerService;
	
	@Autowired
	private AddressService addressService;
	
	@Post
	public String execute(Invocation inv, ItemInfo itemInfo, ItemExtendHelper helper) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		inv.addModel("isPreview", true);
		inv.addModel("deletedMap", this.dictService.makeOptionList(DictConstants.DELETED, "", ""));
		inv.addModel("itemInfo", itemInfo);
		inv.addModel("categoryName", inv.getParameter("categoryName"));
		
		AddressVo originAddress = new AddressVo();
		originAddress.setStrLocationIds(helper.getOriginIds());
		originAddress.setStrLocationNames(helper.getOriginNames());
		inv.addModel("originAddress", originAddress);
		
		List<PackageItem> packageList = JSON.parseArray(helper.getStrSkuArray(), PackageItem.class);
		if(CollectionUtils.isNotEmpty(packageList)){
			List<ItemSku> itemSkuList = Lists.newArrayList();
			for(PackageItem packageItem: packageList){
				ItemSku sku = new ItemSku();
				sku.setId(packageItem.getPackageId());
				sku.setProperties(packageItem.getPackageName());
				sku.setQuantity(packageItem.getPackageQuantity());
				sku.setPrice(packageItem.getPackagePrice());
				itemSkuList.add(sku);
			}
			inv.addModel("itemSkuList", itemSkuList);
		}
		
		List<ImageItem> imageList = JSON.parseArray(helper.getStrImageArray(), ImageItem.class);
		if(CollectionUtils.isNotEmpty(imageList)){
			List<ItemImage> itemImageList = Lists.newArrayList();
			for(ImageItem image: imageList){
				ItemImage itemImage = new ItemImage();
				itemImage.setId(image.getId());
				itemImage.setOriginUrl(image.getSrc());
				itemImageList.add(itemImage);
			}
			inv.addModel("itemImages", itemImageList);
		}
		
		DesignerEntity designer = this.designerService.findDesignerByUserId(userInfo.getId());
		AddressVo designerAddress = this.addressService.getAddressVoByOriginAndOuterId(ResourceConstants.AddressOrigin.SELLER_ADDRESS, userInfo.getId());
		inv.addModel("designerAddress", designerAddress);
		inv.addModel("designer", designer);
		
		List<ItemInfoVo> relatedItemList = this.itemRepository.getRelatedItemList(userInfo.getId(), 0L);
		inv.addModel("relatedItemList", relatedItemList);
		return "ItemDetail";
	}

}
